﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using Faster.GameUtility;
using System.IO;

namespace Faster.GameBLL
{
    public class RequestHelper
    {
        public static string GetRequest(string url,string cookies)
        {
            HttpWebRequest hreq = (HttpWebRequest)HttpWebRequest.Create(url);
            hreq.Method = "GET";
            hreq.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
            hreq.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1) Gecko/20090624 Firefox/3.5"; 
            //hreq.ContentType = "application/x-amf";
            hreq.Referer = "http://s1.kaixin001pvz.youkia.com/swf.003/main.swf?833594617";
            
            hreq.CookieContainer = GetCookieContainer(cookies);

            HttpWebResponse hres = (HttpWebResponse)hreq.GetResponse();
            Stream responseStream = hres.GetResponseStream();
            
            StreamReader streamReader = new StreamReader(responseStream, Encoding.UTF8);
            string html = streamReader.ReadToEnd();
            hreq.Abort();
            return html;
        }

        public static AMFMessage PostRequest(string url, string cookies,byte[] data)
        {
            HttpWebRequest hreq = (HttpWebRequest)HttpWebRequest.Create(url);
            hreq.Method = "POST";
            hreq.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
            hreq.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1) Gecko/20090624 Firefox/3.5";
            hreq.ContentType = "application/x-amf";
            hreq.Referer = "http://s1.kaixin001pvz.youkia.com/swf.003/main.swf?833594617";
            hreq.CookieContainer = GetCookieContainer(cookies);
            hreq.ContentLength = data.Length;
            Stream poststream = hreq.GetRequestStream();
            poststream.Write(data, 0, data.Length);
            poststream.Close();
            HttpWebResponse hres = (HttpWebResponse)hreq.GetResponse();           
            Stream responseStream = hres.GetResponseStream();
            AMFDeserializer deserializer = new AMFDeserializer(responseStream);
           
            AMFMessage message = null;
            try
            {
                message = deserializer.Deserialize();
            }
            catch (Exception ex)
            {

            }
            hreq.Abort();
            return message;
        }

        public static CookieContainer GetCookieContainer(string cookies)
        {
            CookieContainer myCookieContainer = new CookieContainer();
            string[] cookstr = cookies.Split(';');
            foreach (string str in cookstr)
            {
                string[] cookieNameValue = str.Split('=');
                if (cookieNameValue.Length == 2)
                {
                    Cookie ck = new Cookie(cookieNameValue[0].Trim().ToString(), cookieNameValue[1].Trim().ToString());
                    ck.Domain = "s1.kaixin001pvz.youkia.com";
                    myCookieContainer.Add(ck);
                }
            }
            return myCookieContainer;
        }
    }

}
